/*
 * @lc app=leetcode.cn id=953 lang=cpp
 *
 * [953] 验证外星语词典
 */

// @lc code=start
class Solution
{
public:
  bool isAlienSorted(vector<string>& words, string order)
  {
    unordered_map<char, int> alpha;
    for (int i = 0; i < 26; ++i)
      alpha[order[i]] = i;
    return is_sorted(words.begin(), words.end(), [&](const string& f, const string& s) -> bool {
      int m = f.size(), n = s.size();
      for (int i = 0; i < m && i < n; ++i)
        if (alpha[f[i]] < alpha[s[i]])
          return true;
        else if (alpha[f[i]] > alpha[s[i]])
          return false;
      return m < n;
    });
  }
};
// @lc code=end
